package com.xb.system.sharding;

import com.google.common.hash.Hashing;

import java.nio.charset.StandardCharsets;

/**
 * 分片算法验证工具
 * 可独立运行，用于验证 Java 实现是否与 Python mmh3.hash() 一致
 */
public class ShardingVerifier {

    private static final int SHARDING_COUNT = 200;

    public static int getTableIndex(String phone) {
        if (phone == null || phone.isEmpty()) {
            return 0;
        }
        
        int hashValue = Hashing.murmur3_32_fixed()
                .hashString(phone, StandardCharsets.UTF_8)
                .asInt();
        
        return Math.abs(hashValue) % SHARDING_COUNT;
    }

    public static void main(String[] args) {
        // 测试用例 - 必须与 Python 结果一致
        String[] testPhones = {
            "13600005769",  // Python 结果: data_85
            "13800138000",
            "18888888888",
            "15912345678"
        };

        String separator = "============================================================";
        System.out.println(separator);
        System.out.println("MurmurHash3 分片算法验证");
        System.out.println(separator);
        System.out.println();

        for (String phone : testPhones) {
            int hashValue = Hashing.murmur3_32_fixed()
                    .hashString(phone, StandardCharsets.UTF_8)
                    .asInt();
            int tableIndex = getTableIndex(phone);
            
            System.out.printf("电话号码: %s%n", phone);
            System.out.printf("  Hash值: %d%n", hashValue);
            System.out.printf("  表索引: %d%n", tableIndex);
            System.out.printf("  目标表: data_%d%n", tableIndex);
            System.out.println();
        }

        System.out.println(separator);
        System.out.println("请运行 Python 脚本对比验证:");
        System.out.println("  python check_phone_table.py 13600005769");
        System.out.println(separator);
    }
}

